#include <QtGui>
#include "nde.h"
#include "editscreen.h"
#include "configscreen.h"
#include "projectwizard.h"

NDE::NDE(QWidget *parent)
  : QMainWindow(parent) {
  QCoreApplication::setOrganizationName("OPEN-VN");
  QCoreApplication::setOrganizationDomain("open-vn.org");
  QCoreApplication::setApplicationName("NDE");
  QDesktopWidget* desktopWidget = QApplication::desktop();
  int swidth = desktopWidget->width();
  int sheight = desktopWidget->height();
  this->setMinimumSize(swidth / 2, sheight / 2);
  this->setMaximumSize(swidth, sheight);
  QMenu* fileMenu = menuBar()->addMenu("&File");
  QAction* newAct = new QAction("&New", this);
  ProjectWizard* creatProject = new ProjectWizard(this);

  connect(newAct, SIGNAL(triggered()), creatProject, SLOT(exec()));
  fileMenu->addAction(newAct);
  QAction* openProAct = new QAction("&Open project", this);
  fileMenu->addAction(openProAct);
  connect(openProAct, SIGNAL(triggered()), this, SLOT(OpenProject()));

  QMenu* toolMenu = menuBar()->addMenu("&Tools");
  QAction* cfgAct = new QAction("&Options...", this);
  toolMenu->addAction(cfgAct);
  connect(cfgAct, SIGNAL(triggered()), this, SLOT(ShowConfigScreen()));

  editScreen = new EditScreen;
  connect(creatProject, SIGNAL(ProjectCreated(QString)), editScreen,
          SLOT(setProject(QString)));
  cfgScreen = new ConfigScreen;
  connect(cfgScreen, SIGNAL(closed()), this, SLOT(ShowEditScreen()));

  mainwidget = new QStackedWidget(this);
  mainwidget->addWidget(editScreen);
  mainwidget->addWidget(cfgScreen);
  setCentralWidget(mainwidget);
}

NDE::~NDE() {
}
void NDE::OpenProject() {
  QFileDialog* d = new QFileDialog(this);
  d->setNameFilter("NDE Project Manager File(*.ndepro)");
  d->show();
  connect(d, SIGNAL(fileSelected(QString)), editScreen, SLOT(setProject(QString)));
}

void NDE::ShowConfigScreen() {
  mainwidget->setCurrentWidget(cfgScreen);
}

void NDE::ShowEditScreen() {
  mainwidget->setCurrentWidget(editScreen);
}

void NDE::closeEvent(QCloseEvent *e) {
  editScreen->CloseAll();
  QMainWindow::closeEvent(e);
}
